package com.yanggeng.demo.beginnerguidedemo.activity;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.DatePicker;

import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.activity.BaseActivity;
import com.yanggeng.demo.beginnerguidedemo.bean.db.BillBean;
import com.yanggeng.demo.beginnerguidedemo.bean.db.BillDaoUtil;
import com.yanggeng.demo.beginnerguidedemo.bean.db.ImageAddrBean;
import com.yanggeng.demo.beginnerguidedemo.utils.FileUtils;
import com.yanggeng.demo.beginnerguidedemo.utils.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.yanggeng.demo.beginnerguidedemo.bean.db.BillDaoUtil.insert;

/**
 * Created by yanggeng on 2017/7/21.
 */

public class DBActivity extends BaseActivity {

    private static final int REQUEST_FILE = 102;
    private List<ImageAddrBean> imageAddrBeanList;
    private Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_db);
        context = this;
        setTitle(true, "数据库测试");
        findViewById(R.id.btn_insert1).setOnClickListener(this);
        findViewById(R.id.btn_insert2).setOnClickListener(this);
        findViewById(R.id.btn_find).setOnClickListener(this);
        findViewById(R.id.btn_date).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_insert1:
                BillBean billBean = new BillBean(null, "吃饭", String.valueOf(Math.random()), "吃的KFC", new Date().toString());
                insert(billBean, null);
                break;

            case R.id.btn_insert2:
                selectFile();

                break;

            case R.id.btn_find:
                ToastUtil.centerToast(context, BillDaoUtil.findAll().toString());
                break;

            case R.id.btn_date:
                showDatePickerDialog();
                break;
        }
    }

    private void selectFile() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, REQUEST_FILE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_FILE && resultCode == RESULT_OK) {
            Uri uri = data.getData();
            ToastUtil.centerToast(context, "选择文件路径：" + uri.toString());
            String path = FileUtils.getPath2(context, uri);
            BillBean billBean1 = new BillBean(null, "看电影", String.valueOf(Math.random()), "悟空传", new Date().toString());
            imageAddrBeanList = new ArrayList<>();
            imageAddrBeanList.add(new ImageAddrBean(null, null, path));
            BillDaoUtil.insert(billBean1, imageAddrBeanList);
        }
    }

    private void showDatePickerDialog() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                ToastUtil.centerToast(context, "date: " + year + "-" + (month + 1) + "-" + dayOfMonth);
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }
}
